package iterator;

public class ArrayList_ implements Collection_{

    private Object [] list =  new Object[3];
    private int size = 0;

    @Override
    public Collection_ add(Object o) {
        if(size == list.length){
            Object [] kr = new Object[size * 2];
            System.arraycopy(list,0, kr,0,list.length);
            list = kr;
            System.out.println("扩容");
        }

        list[size++] = o;

        return this;
    }

    @Override
    public int size() {
        return size;
    }

    // 返回迭代器
    @Override
    public Iterator_ getIterator(){
        return new Iterator_() {
            private int readFlg = 0;
            @Override
            public boolean hasNext() {
                return readFlg < size;
            }

            @Override
            public Object next() {
                return list[readFlg++];
            }
        };
    }
}
